<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<head>
<title>_库存移位</title>
<script type="text/javascript">
var stocks = [];
$(function() {
		$("#queryForm").validate(); 
});
	
function query(){
	$("#queryForm").submit();
}
//锁定当前记录
function lock(item){
	if($("#n_sku_"+item).length > 0){
		alert('已锁定一条记录,请先处理完成');
	}
	stocks = [];
	var stock = {
		customerCode:$("#customerCode").val(),
		warehouseCode:$("#warehouseCode").val(),
		sku:$("#sku").val(),
		batchCode:$("#batchCode_"+item).html(),
		productDate:$("#productDate_"+item).html(),
		expireDate:$("#expireDate_"+item).html(),
		stockCode:$("#stockCode_"+item).html(),
		boxCode:$("#boxCode_"+item).html(),
		salesNum:$("#salesNum_"+item).html(),
		moveFlg:'from'
	};
	$("#detailBody").html("");
	var detailHtml = "";
	detailHtml += "<tr>";
	detailHtml += "<td>1</td>";
	detailHtml += "<td>"+$("#customerName_"+item).html()+"</td>";
	detailHtml += "<td>"+$("#warehouseCode_"+item).html()+"</td>";
	detailHtml += "<td id='n_sku_"+item+"'>"+$("#sku_"+item).html()+"</td>";
	detailHtml += "<td id='n_batchCode_"+item+"'>"+$("#batchCode_"+item).html()+"</td>";
	detailHtml += "<td id='n_productDate_"+item+"'>"+$("#productDate_"+item).html()+"</td>";
	detailHtml += "<td id='n_expireDate_"+item+"'>"+$("#expireDate_"+item).html()+"</td>";
	detailHtml += "<td><input id='n_salesNum_"+item+"' type='text'></input></td>";
	detailHtml += "<td><input id='n_stockCode_"+item+"' type='text' value='"+$("#stockCode_"+item).html()+"'></input></td>";
	detailHtml += "<td><input id='n_boxCode_"+item+"' type='text' value='"+$("#boxCode_"+item).html()+"'></input></td>";
	detailHtml += "<td><button type='button' class='btn btn-danger' onclick='move(\""+item+"\",this)'>移动</button></td>";
	detailHtml += "</tr>";
	$("#detailBody").html(detailHtml);
	stocks.push(stock);
}
//移动库位
function move(item,btn){
	  var nSalesNum = $("#n_salesNum_"+item).val();
	  if(!nSalesNum || isNaN(nSalesNum)){
		alert("sku数量输入错误.");
		return;
		}
	  if(parseInt(nSalesNum) > parseInt($("#salesNum_"+item).html())){
		alert("移动的数量不能超过原数量");
		return;
	  }
	
	  var boxCode=$("#n_boxCode_"+item).val().trim();
	  var stockCode=$("#n_stockCode_"+item).val().trim();
	  var t = /^[A-Z0-9\.-]*$/g;
	  var t1 = /^[A-Z0-9\.-]*$/g;
	  
	  if(!t.test(boxCode)){
		jAlert("箱编码只能输入大写字母,数字,中划线.");
		return;
	  }
		  
	  if(!t1.test(stockCode)){
		jAlert("库位编码只能输入大写字母,数字,中划线.");
		return;
	  }
	
	var stock = {
		customerCode:$("#customerCode").val(),
		warehouseCode:$("#warehouseCode").val(),
		sku:$("#sku").val(),
		batchCode:$("#n_batchCode_"+item).html(),
		productDate:$("#n_productDate_"+item).html(),
		expireDate:$("#n_expireDate_"+item).html(),
		salesNum:$("#n_salesNum_"+item).val(),
		stockCode:stockCode,
		boxCode:boxCode,
		moveFlg:'to'
	};
	stocks.push(stock);
	
	$.ajax({ 
       type: "post",
       contentType: "application/json", 
       url: "${ctx}/v2_moveStorage/move",    
       dataType: "json",   
       data: JSON.stringify(stocks),
       success : function(data) {
       		jAlert(data.content);
       		query();
		},
	    error : function(data) {
			alert("操作失败，请检查该sku是否还有未处理完成的订单");
			$(btn).attr('disabled',true);
		}
   });
}
</script>
</head>
<body>
<section class="content">
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-search"></i>
					<h3 class="box-title">搜索栏</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse" type="button">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				<form class="form-search" id="queryForm" action="${ctx}/v2_moveStorage/query" method="post">
					<div class="form-group row">
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 商家： </span> 
								<select class="form-control required" id="customerCode" name="customerCode" onchange="ajaxAreaGroup(this,'warehouseCode')">
									<option value=""></option>
									<c:forEach items="${customerList}" var="customer" >
										<option value="${customer.customerCode}"
											<c:if test="${param['customerCode'] eq customer.customerCode}">selected=selected</c:if>>${customer.customerName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> 仓库： </span> 
								<select class="form-control span3 required" id="warehouseCode" name="warehouseCode">
									<option value=""></option>
									<c:forEach items="${warehouseList}" var="warehouse">
										<option value="${warehouse.groupCode}"
											<c:if test="${param.warehouseCode eq warehouse.groupCode }">selected=selected</c:if>>${warehouse.groupName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<span class="input-group-text"> SKU： </span> 
								<input class="form-control span3 required" id="sku" name="sku" value="${param.sku }"/>
							</div>
						</div>
						<div class="col-sm-3">
							<div class="input-group col-sm-12">
								<button class="btn btn-info" type="button" onclick="query()">查询</button>
							</div>
						</div>

					</div>
				</form>
				</div>

			</div>
			<!-- /.box-body -->

		</div>
		<!-- /.col -->
	</div>
	<!-- /.row -->

	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
				<i class="fa fa-list"></i>
					<h3 class="box-title">原始库位</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div></div>
				<div class="box-body">
				<form id="dataForm">
					<table id="orgTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th>序号</th>
								<th>商家</th>
								<th>仓库</th>
								<th>SKU</th>
								<th>批次号</th>
								<th>生产日期</th>
								<th>过期日期</th>
								<th>库存</th>
								<th>库位</th>
								<th>箱编码</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
						<c:forEach items="${cusStocks}" var="o" varStatus="i">
						<tr>
			   				<td>${i.count}</td>
			   				<td id="customerName_${o.id }">${o.customer.customerName}</td>
			   				<td id="warehouseCode_${o.id }">
				   				<c:forEach items="${warehouseList}" var="warehouse">
					   				<c:if test="${warehouse.groupCode eq o.warehouseCode}">
					   					${warehouse.groupName}
					   				</c:if>
								</c:forEach>
			   				</td>
			   				<td id="sku_${o.id }">${o.sku}</td>
			   				<td id="batchCode_${o.id }">${o.batchCode}</td>
			   				<td id="productDate_${o.id }">${o.productDate}</td>
			   				<td id="expireDate_${o.id }">${o.expireDate}</td>
			   				<td id="salesNum_${o.id }">${o.salesNum}</td>
			   				<td id="stockCode_${o.id }">${o.stockCode }</td>
			   				<td id="boxCode_${o.id }">${o.boxCode }</td>
			   				<td><button class="btn btn-primary" type="button" onclick="lock('${o.id}')">锁定</button></td>
			   			</tr>
						</c:forEach>
						</tbody>
					</table>
					</form>
				</div>
			</div>
			<!-- /.box -->
		</div>
		<!-- /.col -->
	</div>
	
	<div class="form-group row">
		<div class="col-md-12">
			<div class="box box-solid box-primary">
				<div class="box-header">
				<i class="fa fa-list"></i>
					<h3 class="box-title">新库位</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th>序号</th>
								<th>商家</th>
								<th>仓库</th>
								<th>SKU</th>
								<th>批次号</th>
								<th>生产日期</th>
								<th>过期日期</th>
								<th>库存</th>
								<th>库位</th>
								<th>箱编码</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody id="detailBody">
						</tbody>
					</table>
				</div>
			</div>
			<!-- /.box -->
		</div>
		<!-- /.col -->
	</div>
	<!-- /.row -->
</section>	
</body>
</html>